{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Counter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A simple progress counter to print the number of iterations and time elapsed in a for-loop execution."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "> from mlxtend.utils import Counter"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Overview"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The `Counter` class implements an object for displaying the number of iterations and time elapsed in a for-loop. Please note that the `Counter` was implemented for efficiency; thus, the `Counter` offers only very basic functionality in order to avoid relatively expensive evaluations (of if-else statements)."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### References\n",
    "\n",
    "- -"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example 1 - Counting the iterations in a for-loop"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from mlxtend.utils import Counter"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "20 iter | 2 sec"
     ]
    }
   ],
   "source": [
    "import time\n",
    "\n",
    "cnt = Counter()\n",
    "for i in range(20):\n",
    "    # do some computation\n",
    "    time.sleep(0.1)\n",
    "    cnt.update()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that the first number displays the current iteration, and the second number shows the time elapsed after initializing the `Counter`."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## API"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "## Counter\n",
      "\n",
      "*Counter(stderr=False, start_newline=True, precision=0, name=None)*\n",
      "\n",
      "Class to display the progress of for-loop iterators.\n",
      "\n",
      "**Parameters**\n",
      "\n",
      "- `stderr` : bool (default: True)\n",
      "\n",
      "    Prints output to sys.stderr if True; uses sys.stdout otherwise.\n",
      "\n",
      "- `start_newline` : bool (default: True)\n",
      "\n",
      "    Prepends a new line to the counter, which prevents overwriting counters\n",
      "    if multiple counters are printed in succession.\n",
      "    precision: int (default: 0)\n",
      "    Sets the number of decimal places when displaying the time elapsed in\n",
      "    seconds.\n",
      "\n",
      "- `name` : string (default: None)\n",
      "\n",
      "    Prepends the specified name before the counter to allow distinguishing\n",
      "    between multiple counters.\n",
      "\n",
      "**Attributes**\n",
      "\n",
      "- `curr_iter` : int\n",
      "\n",
      "    The current iteration.\n",
      "\n",
      "- `start_time` : float\n",
      "\n",
      "    The system's time in seconds when the Counter was initialized.\n",
      "\n",
      "- `end_time` : float\n",
      "\n",
      "    The system's time in seconds when the Counter was last updated.\n",
      "\n",
      "**Examples**\n",
      "\n",
      "\n",
      "    >>> cnt = Counter()\n",
      "    >>> for i in range(20):\n",
      "    ...     # do some computation\n",
      "    ...     time.sleep(0.1)\n",
      "    ...     cnt.update()\n",
      "    20 iter | 2 sec\n",
      "    >>> print('The counter was initialized.'\n",
      "    ' %d seconds ago.' % (time.time() - cnt.start_time))\n",
      "    The counter was initialized 2 seconds ago\n",
      "    >>> print('The counter was last updated'\n",
      "    ' %d seconds ago.' % (time.time() - cnt.end_time))\n",
      "    The counter was last updated 0 seconds ago.\n",
      "\n",
      "For more usage examples, please see\n",
      "[http://rasbt.github.io/mlxtend/user_guide/utils/Counter/](http://rasbt.github.io/mlxtend/user_guide/utils/Counter/)\n",
      "\n",
      "### Methods\n",
      "\n",
      "<hr>\n",
      "\n",
      "*update()*\n",
      "\n",
      "Print current iteration and time elapsed.\n",
      "\n",
      "\n"
     ]
    }
   ],
   "source": [
    "with open('../../api_modules/mlxtend.utils/Counter.md', 'r') as f:\n",
    "    print(f.read())"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  },
  "toc": {
   "nav_menu": {},
   "number_sections": false,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {},
   "toc_section_display": true,
   "toc_window_display": false
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
